package customfields;

import log.Logger;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import util.LookAndFeel;

public class OptionField extends Field{
	
	private String text;
	private int height;
	private int width;
	private int padding = 10;
	private int textColorUnfocused = Color.BLACK;
	private int textColorFocused = Color.WHITE;
	private int textColor;
	private Bitmap arrow = Bitmap.getBitmapResource(LookAndFeel.arrow);
	
	public OptionField(String txt, long style){
		super(style);
		this.text = txt;
		height = (int) (getFont().getHeight()*1.5);  
		width = (int) (Display.getWidth()*0.75); 
	}
	
	public OptionField(String txt, long style, int w, int h){
		super(style);
		this.text = txt;
		height = w;  
		width = h; 
	}
	
	public OptionField(String txt){
		super();
		this.text=txt;
		height = (int) (getFont().getHeight()*1.5);  
		width = (int) (Display.getWidth()*0.75); 
	}

	public int getPreferredWidth() {  
		return width;  
	}  

	public int getPreferredHeight() {  
		return height;  
	}  
	
	protected void onFocus(int direction) {
		textColor = textColorFocused; 
		invalidate();  
	}  

	protected void onUnfocus() {  
		textColor = textColorUnfocused;  
		invalidate();  
	} 

	protected void layout(int arg0, int arg1) {  
		setExtent(getPreferredWidth(), getPreferredHeight());  
	}  

	
	protected void fieldChangeNotify(int context) {
		try {
			this.getChangeListener().fieldChanged(this, context);
		}
		catch (Exception e) {
			Logger.log("field error");
			
		}
	}


	protected boolean navigationClick(int status, int time) {
		fieldChangeNotify(1);
		return true;
	}
	
	protected void paint(Graphics graphics) {
		// text
		graphics.setColor(textColor);
		graphics.drawText(text,padding,padding/2);  
		graphics.drawBitmap(width-30,(height-18)/2,25,18,arrow,0,0);
	}

}

